package ui.magic.magiclibs.container.Linear;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.Vector;

import ui.magic.magiclibs.container.Base.Percent;
import ui.magic.magiclibs.container.Base.MagicInterface;
import ui.magic.magiclibs.container.Base.MagicType;

/**
 * Created by liushiyin on 2017/10/21.
 */

public class LinearMagicUi extends LinearMagicNull {

  /********************以下参数用于指定父节点中子节点的对齐方式*****************/
    public static int LEFT=Gravity.LEFT;
    public static int RIGHT=Gravity.RIGHT;
    public static int TOP=Gravity.TOP;
    public static int BOTTOM=Gravity.BOTTOM;
    public static int CENTER=Gravity.CENTER;
  /****************************************************************************/
    private Vector<View> childList;
    private Vector<Percent> percentList;
    private MagicType type;

    public LinearMagicUi(Context context) {
        super(context);
        init(MagicType.As_Child);
    }

    public LinearMagicUi(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(MagicType.As_Child);
    }

    public LinearMagicUi(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(MagicType.As_Child);
    }

    /*
    *设置字节点的对齐方式
    *@Param align：子节点的对齐方式，可选参数为LEFT、RIGHT、TOP、BOTTOM、CENTER，
    * 也可以通过“|”操作符结合使用
     */
    public void setAlign(int align){
        this.setGravity(align);
    }

    /*
  *进行一些初始化操作
   */
    private void init(MagicType type) {
        this.type=type;
        childList=new Vector<>();
        percentList=new Vector<>();
        setMagicSize(getWindowSize().x,getWindowSize().y);
        setAlign(CENTER);
    }

    /*
   *在组件大小改变时，调整子节点大小
    */
    private void reOrderChildSize(){
        int count=childList.size();
        for(int i=0;i<count;++i){
            Percent percent=percentList.get(i);
            View v=childList.get(i);
           // change(v,percent.perx,percent.pery);
            super.removeView(v);
            addChildTmp(v,percent,-2);
        }
    }

    /*
  *设置UI组件大小，同时会调整子组件大小，自己不要轻易调用此函数，
  *而应有UI组件自身在适当的时候调用
  *@Param width：宽度
  *@Param height：高度
   */
    @Override
    public void setMagicSize(int width, int height) {
        super.setMagicSize(width, height);
        reOrderChildSize();
    }

    /*
  *添加View子节点
  * @Param v：View子节点
  * @Param perWidth：0~100的浮点数表示子节点宽度为父节点宽度的百分比值
  * @Param perHeight：0~100的浮点数表示子节点宽度为父节点高度的百分比值
   */
    public void addChild(View v, float perWidth, float perHeight){
        Percent percent=new Percent(perWidth,perHeight);
        addChildTmp(v,percent,-1);
        childList.add(v);
        percentList.add(new Percent(perWidth,perHeight));
    }

    /*
    *addChildView的中间过程
     */
    private void addChildTmp(View child,Percent percent,int index){
        float dxw=percent.perx/MAX;
        float dxh=percent.pery/MAX;
        int realWidth=(int)(magicSize.x*dxw);
        int realHeight=(int)(magicSize.y*dxh);
        if(child instanceof MagicInterface){
            ((MagicInterface)child).setMagicSize(realWidth,realHeight);
        }
        LayoutParams params=new LayoutParams(realWidth,realHeight);
        params.setMargins(percent.left,percent.top,percent.right,percent.bottom);
        super.addView(child,index,params);
    }

    /*
   *移除指定的View字节点
   * @Param v：指定的view节点
   * @return boolean：成功返回true，失败返回false
    */
    public boolean removeChild(View child) {
        int index = childList.indexOf(child);
        if(index!=-1) {
            super.removeView(child);
            childList.remove(index);
            percentList.remove(index);
            return true;
        }else{
            return false;
        }
    }

    /*
    *获取字节点信息
     */
    public Percent getChildPerInfo(View child){
        int index=childList.indexOf(child);
        if(index!=-1){
            return percentList.get(index);
        }else{
            return null;
        }
    }

    /*
    *改变字节点大小
     */
    public boolean changeChildSize(View child,float perWidth,float perHeight){
        int index=this.indexOfChild(child);
        if(index!=-1){
            super.removeView(child);
            Percent percent=percentList.get(index);
            percent.perx=perWidth;
            percent.pery=perHeight;
            //change(child,perWidth,perHeight);
            addChildTmp(child,percent,index);
            return true;
        }else{
            return false;
        }
    }

    /*
    *添加分割栏默认为透明
     */
    public  void addSplitBar(float perWidth,float perHeight){
        addSplitBar(perWidth,perHeight,0x00000000);
    }

    public void addSplitBar(float perWidth,float perHeight,int color){
        LinearMagicNull linearMagicNull=new LinearMagicNull(getContext());
        linearMagicNull.setBackgroundColor(color);
        this.addChild(linearMagicNull,perWidth,perHeight);
    }

//    private void change(View child,float perWidth,float perHeight){
//        float dxw=perWidth/MAX;
//        float dxh=perHeight/MAX;
//        int realWidth=(int)(magicSize.x*dxw);
//        int realHeight=(int)(magicSize.y*dxh);
//        int[] location=new int[2];
//        child.getLocationInWindow(location);
//        child.layout(location[0],location[1],location[0]+realWidth,location[1]+realHeight);
//        if(child instanceof MagicInterface){
//            ((MagicInterface)child).setMagicSize(realWidth,realHeight);
//        }
//    }

    /*
    *设置子节点的Margin
     */
    public void setChildMargin(View child,int left,int top,int right,int bottom){
        int index=childList.indexOf(child);
        if(index!=-1){
            LayoutParams params=(LayoutParams) child.getLayoutParams();
            params.setMargins(left,top,right,bottom);
            Percent percent=percentList.get(index);
            percent.left=left;
            percent.top=top;
            percent.right=right;
            percent.bottom=bottom;
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(type==MagicType.Self_Init){
            int width=getMeasuredWidth();
            int height=getMeasuredHeight();
            if(magicSize.x!=width||magicSize.y!=height){
                setMagicSize(width,height);
            }
        }
    }

    public MagicType getType(){
        return type;
    }

    public void setSelfInit(){
        type=MagicType.Self_Init;
    }

    public void setAsChild(){
        type=MagicType.As_Child;
    }

    /*
    *设置布局方式为水平方向
     */
    public void setHorizontal(){
        this.setOrientation(LinearLayout.HORIZONTAL);
    }

    /*
    *设置布局方式为垂直方式
     */
    public void setVertical(){
        this.setOrientation(LinearLayout.VERTICAL);
    }

    //一下函数标记为废弃，不建议使用
    @Override
    @Deprecated
    public void addView(View child) {super.addView(child);}
    @Override
    @Deprecated
    public void addView(View child, int index) {super.addView(child, index);}
    @Override
    @Deprecated
    public void addView(View child, int width, int height) {super.addView(child, width, height);}
    @Override
    @Deprecated
    public void addView(View child, ViewGroup.LayoutParams params) {super.addView(child, params);}
    @Override
    @Deprecated
    public void addView(View child, int index, ViewGroup.LayoutParams params) {super.addView(child, index, params);}
    @Override
    @Deprecated
    public void removeView(View view) {super.removeView(view);}
    @Override
    @Deprecated
    public void removeViewAt(int index) {super.removeViewAt(index);}
    @Override
    @Deprecated
    public void removeViews(int start, int count) {super.removeViews(start, count);}
    @Override
    @Deprecated
    public void removeAllViews() {super.removeAllViews();}
}
